const http = require('http')

const { createProxyMiddleware } = require('http-proxy-middleware')

const server = http.createServer((req, res) => {
  let url = req.url
  res.writeHead(200, {
    'Access-Control-Allow-Origin': '*'
  })
  if(/^\/ajax/.test(url)) {
    let apiProxy = createProxyMiddleware(
      '/ajax', {
        target: 'https://m.maoyan.com',
      //  'https://m.maoyan.com/ajax/moreClassicList?sortId=1&showType=3&limit=10&offset=20&optimus_uuid=3D41C7E0832211EBACF0417467C3FD9D5BCA506FAA074FA8900AAE88DDD80A25&optimus_risk_level=71&optimus_code=10'
        changeOrigin: true,
        // pathRewrite: {
        //   '^/ajax': ''
        // }
      }
    )
    apiProxy(req, res) // 正向代理
  } else {
    switch (url) {
      case '/index.html':
        res.end('index.html')
        break
      case '/search.html':
        res.end('search.html')
        break
      default:
        res.end('[404]page not found.')
    }
  }
})
/*
  switch(true) {
    case url === '/api/data':
  }
*/ 

server.listen(8080, () => {
  console.log('localhost:8080')
})